package Controleur;

import java.text.DateFormat;
import java.util.Locale;
import javax.servlet.ServletContext;
import javax.servlet.http.*;

import JavaBean.BeanRepresentation;
import Modele.ListeRepresentations;
import Modele.Representation;

public class RepresentationServlet extends CustomServlet
{
	private static final long serialVersionUID = 1L;

	public void executeGet(HttpServletRequest request, HttpServletResponse response )
	{
		if (request.getParameter("IdRepresentation") != null)
		{
			int id = Integer.parseInt((String)request.getParameter("IdRepresentation"));
			ListeRepresentations representations = (ListeRepresentations)getServletContext().getAttribute("listeRepresentations");
			Representation r = representations.getRepresentation(id);
			
			if ( request.getParameter("qte") != null )
			{
				request.setAttribute ( "layout", null );
				try
				{					
					response.getOutputStream().print(r.getNbBilletsDisponibles() + "");
				}
				catch ( Exception e )
				{	
				}
			}
			else
			{
				BeanRepresentation beanEnvoi = new BeanRepresentation();				
				DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.CANADA_FRENCH);				
				
				beanEnvoi.setArtiste(r.getSpectacle().getArtiste().getNom());
				beanEnvoi.setTitre(r.getTitre());
				beanEnvoi.setEmplacement(r.getEmplacement().getNom());				
				beanEnvoi.setDate(df.format(r.getDate().getTime()));
				beanEnvoi.setIdRepresentation(r.getIdRepresentation());
				beanEnvoi.setNbBilletsDisponibles(r.getNbBilletsDisponibles());
				
				request.setAttribute("Representation", beanEnvoi);
			}
		}
	}

	@Override
	public void executePost(HttpServletRequest request, HttpServletResponse response) 
	{
		executeGet ( request, response );		
	}
}
